package com.homework.dao.impl;

import com.homework.dao.RoleDao;
import com.homework.model.Permission;
import com.homework.model.Role;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author HLQ
 * @date 2024/9/29 11:15
 */
@Slf4j
@Repository
public class RoleDaoImpl implements RoleDao {
    @Autowired
    private Connection conn;
    @Autowired
    private Role role;
    @Override
    public Integer insert(Role role){

        String sql = "INSERT INTO tb_role" + "(role_name,description,status,create_time)"
                + "VALUES" + "(?,?,?,?)" ;
        try(
                PreparedStatement ps = conn.prepareStatement(sql);
                ){
            ps.setString(1,role.getRoleName());
            ps.setString(2,role.getDescription());
            ps.setInt(3,role.getStatus());
            ps.setTimestamp(4, role.getCreateTime());
            return ps.executeUpdate();
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return 0;
    };
    @Override
    public Integer update(Role role){
        String sql = "UPDATE tb_role SET description=?,status=?,update_time=? WHERE role_name=?";
        try(
                PreparedStatement ps = conn.prepareStatement(sql);
        ){
            ps.setString(1,role.getDescription());
            ps.setInt(2,role.getStatus());
            ps.setTimestamp(3, role.getUpdateTime());
            ps.setString(4,role.getRoleName());
            return ps.executeUpdate();
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return 0;
    };
    @Override
    public  Integer delete(String roleName){
        String sql = "DELETE FROM tb_role WHERE role_name=?";
        try(
                PreparedStatement ps = conn.prepareStatement(sql);
                ){
            ps.setString(1,roleName);
            return ps.executeUpdate();
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return 0;
    }

    //如何查询返回的结果
    @Override
    public  List<Role> selectRole(String roleName){
        String sql = "SELECT * FROM tb_role WHERE role_name=?";
        List<Role> roleList = new ArrayList<>();
        try(
                PreparedStatement ps = conn.prepareStatement(sql);
                ){
            ps.setString(1,roleName);
            ResultSet rs = ps.executeQuery();
            while  (rs.next()) {
                role.setRoleId(rs.getInt("role_id"));
                role.setRoleName(rs.getString("role_name"));
                role.setDescription(rs.getNString("description"));
                role.setStatus(rs.getInt("status"));
                role.setCreateTime(rs.getTimestamp("create_time"));
                role.setUpdateTime(rs.getTimestamp("update_time"));
                roleList.add(role);
            }
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return roleList;
    }
}
